package com.example.test.chapter14;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class NotesDbAdapter {
	public static final String KEY_TITLE = "title";
	public static final String KEY_BODY = "body";
	public static final String KEY_ROWID = "_id";
	private static final String TAG = "NotesDbAdapter";

	private static final String DATABASE_NAME = "date";
	private static final String DATABASE_TABLE = "notes";
	private static final int DATABASE_VER = 2;
	private static final String DATABASE_CREATE = "create table notes (_id integer primary key autoincrement, "
			+ "title text not null, body text not null);";
	private DatabaseHelper databaseHelper;
	private SQLiteDatabase liteDatabase;
	private Context mContext;

	private static class DatabaseHelper extends SQLiteOpenHelper {
		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VER);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS notes");
			onCreate(db);
		}
	}

	public NotesDbAdapter(Context context) {
		this.mContext = context;
	}

	public NotesDbAdapter open() throws SQLException {
		databaseHelper = new DatabaseHelper(mContext);
		liteDatabase = databaseHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		databaseHelper.close();
	}

	public long createNote(String title, String body) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_TITLE, title);
		contentValues.put(KEY_BODY, body);
		return liteDatabase.insert(DATABASE_TABLE, null, contentValues);
	}

	public boolean deleteNote(long _id) {
		return liteDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + _id, null) > 0;
	}

	public Cursor fechtAllNotes() {
		return liteDatabase.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_TITLE, KEY_BODY }, null, null, null, null, null);
	}

	public Cursor fetchNote(long rowId) {
		Cursor cursor = liteDatabase.query(DATABASE_TABLE, new String[] {
				KEY_ROWID, KEY_TITLE, KEY_BODY }, KEY_ROWID + " = " + rowId,
				null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}

	public boolean updateNote(long rowId, String title, String body) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_TITLE, title);
		contentValues.put(KEY_BODY, body);
		return liteDatabase.update(DATABASE_TABLE, contentValues, KEY_ROWID
				+ "=" + rowId, null) > 0;
	}
}
